package com.study.service.impl;

import com.study.mapper.LogMapper;
import com.study.mapper.RoleResourcesMapper;
import com.study.mapper.UserRoleMapper;
import com.study.model.Logs;
import com.study.model.RoleResources;
import com.study.service.RoleResourcesService;
import com.study.shiro.MyShiroRealm;
import com.study.shiro.ShiroService;
import com.study.util.MyDate;
import com.study.util.getUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author king james
 * @create 2021-09-03-{TIME}
 */
@Service("roleResourcesService")
public class RoleResourcesServiceImpl extends BaseService<RoleResources> implements RoleResourcesService {
    @Resource
    private UserRoleMapper userRoleMapper;
    /*@Resource
    private ShiroService shiroService;*/
    @Autowired
    private MyShiroRealm myShiroRealm;

    @Autowired
    private RoleResourcesMapper resourcesMapper;

    @Autowired
    private LogMapper logMapper;



    @Override
    //更新权限
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    //@CacheEvict(cacheNames="resources", allEntries=true)
    public void addRoleResources(RoleResources roleResources) {
        //删除
        Example example = new Example(RoleResources.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid",roleResources.getRoleid());
        mapper.deleteByExample(example);
        //添加
        if(!StringUtils.isEmpty(roleResources.getResourcesid())){
            String[] resourcesArr = roleResources.getResourcesid().split(",");

            String resources="";
            //角色名
           String roleName = resourcesMapper.queryRoleName(roleResources.getRoleid());
            for(String resourcesId:resourcesArr ){
                RoleResources r = new RoleResources();
                r.setRoleid(roleResources.getRoleid());
                r.setResourcesid(resourcesId);
                //获取权限资源名
                resources = resources +  resourcesMapper.queryResourcesName(resourcesId) + ",";
                mapper.insert(r);
            }

            //记录日志
            getUserInfo getUserInfo = new getUserInfo(); //用户信息
            MyDate myDate = new MyDate();//当前系统时间
            Logs logs = new Logs();
            logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t为角色:" + roleName +"分配了:" + resources + "\t等权限");
            logs.setCreateTime(myDate.getDate());
            logMapper.addLog(logs);

        }

        List<Integer> userIds= userRoleMapper.findUserIdByRoleId(roleResources.getRoleid());
        //更新当前登录的用户的权限缓存
        myShiroRealm.clearUserAuthByUserId(userIds);
    }
}
